package cn.zwxky.android.staff.Approve;


import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.Calendar;
import java.util.HashMap;

import cn.zwxky.android.staff.Login.ForgetBean;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.NoDoubleClickUtils;
import cn.zwxky.android.staff.Utils.URLString;

/**
 * 请假申请
 * Created by Administrator on 2017/3/29 0029.
 */
@ContentView(R.layout.aty_leave_add)
public class LeaveAddActivity extends LsfbActivity {

    /**
     * 开始时间
     */
    @ViewInject(R.id.aty_leave_add_start_text_time)
    private TextView start_text_time;

    /**
     * 结束时间
     */
    @ViewInject(R.id.aty_leave_add_end_text_time)
    private TextView end_text_time;

    /**
     * 请假原因
     */
    @ViewInject(R.id.aty_leave_add_et_import)
    private EditText et_import;

    /**
     * 审批人
     */
    @ViewInject(R.id.aty_leave_add_img_select)
    private SimpleDraweeView img_select;


    /**
     * 记录日期
     */
    private String start_riqi = "";

    /**
     * 记录时间
     */
    private String start_time = "";

    private DatePickerDialog datePickerDialog1;

    /**
     * 日期选择器变量
     */
    private Calendar start_showDate;

    /**
     * 时间选择器变量
     */
    private Calendar start_showTime;

    /**
     * 记录日期
     */
    private String end_riqi = "";

    /**
     * 记录时间
     */
    private String end_time = "";

    private DatePickerDialog datePickerDialog2;

    /**
     * 日期选择器变量
     */
    private Calendar end_showDate;

    /**
     * 时间选择器变量
     */
    private Calendar end_showTime;

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1000:
                    start_text_time.setText("开始时间：" + start_time);
                    break;
                case 2000:
                    end_text_time.setText("结束时间：" + end_time);
                    break;
            }
        }
    };

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "请假审批");
        TextView textView = new TextView(this);
        textView.setText("提交");
        textView.setTextColor(getResources().getColor(R.color.white_color));
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (et_import.getText().toString().isEmpty()) {
                    SN.SnackShow(et_import, "请输入请假原因");
                    return;
                }
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    post();
                }

            }
        });
        getTitleBar().setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);

        start_showDate = Calendar.getInstance();
        start_showTime = Calendar.getInstance();
        end_showDate = Calendar.getInstance();
        end_showTime = Calendar.getInstance();
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_leave_add_start_time, R.id.aty_leave_add_end_time})
    public void onClick(View view) {
        switch (view.getId()) {
            //开始时间
            case R.id.aty_leave_add_start_time:

                showStartDateDialog();
                break;
            //结束时间
            case R.id.aty_leave_add_end_time:
                showSEndDateDialog();

                break;
        }
    }

    @Override
    public void initData() {
        getData();
    }

    /**
     * 获取审批人头像
     */
    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        new BaseInternet().getData_Get(URLString.LEADER, hashMap, ApproverBean.class, new ApproverEvent<ApproverBean>(), true, true);
    }

    @EventAnnotation
    public void ApproverEvent(ApproverEvent<ApproverBean> event) {
        switch (event.getCode()) {
            case 200:
                img_select.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getImg()));
                break;
            default:
                break;
        }
    }


    public void post() {
        if (start_time.isEmpty()) {
            SN.SnackShow(et_import, "请选择请假开始时间");
            return;
        }
        if (end_time.isEmpty()) {
            SN.SnackShow(et_import, "请选择请假结束时间");
            return;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("content", et_import.getText().toString());
        hashMap.put("start", start_time);
        hashMap.put("end", end_time);
        new BaseInternet().getData(URLString.LEAVE_PLZ, hashMap, ForgetBean.class, new LeavePostEvent<ForgetBean>(), false);
    }

    @EventAnnotation
    public void LeavePostEvent(LeavePostEvent<ForgetBean> event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, event.getMsg());
                finish();
                break;
            default:
                SN.SnackShow(et_import, event.getMsg());
                break;
        }
    }


    /**
     * 开始时间选择器
     */
    private void showStartDateDialog() {
        //日期选择器
        datePickerDialog1 = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                start_showDate.set(Calendar.YEAR, year);
                start_showDate.set(Calendar.MONTH, monthOfYear);
                start_showDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                start_riqi = (String) DateFormat.format("yyyy-MM-dd", start_showDate);

                new TimePickerDialog(LeaveAddActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        start_showTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        start_showTime.set(Calendar.MINUTE, minute);
                        start_time = start_riqi + "  " + DateFormat.format("HH:mm", start_showTime);
                        handler.sendEmptyMessage(1000);
                    }
                }, start_showTime.get(Calendar.HOUR_OF_DAY), start_showTime.get(Calendar.MINUTE), true).show();


            }
        }, start_showDate.get(Calendar.YEAR), start_showDate.get(Calendar.MONTH), start_showDate.get(Calendar.DAY_OF_MONTH));
        datePickerDialog1.show();
    }

    /**
     * 结束时间选择器
     */
    private void showSEndDateDialog() {
        //日期选择器
        datePickerDialog2 = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                end_showDate.set(Calendar.YEAR, year);
                end_showDate.set(Calendar.MONTH, monthOfYear);
                end_showDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                end_riqi = (String) DateFormat.format("yyyy-MM-dd", end_showDate);

                new TimePickerDialog(LeaveAddActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        end_showTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        end_showTime.set(Calendar.MINUTE, minute);
                        end_time = end_riqi + "  " + DateFormat.format("HH:mm", end_showTime);
                        handler.sendEmptyMessage(2000);
                    }
                }, end_showTime.get(Calendar.HOUR_OF_DAY), end_showTime.get(Calendar.MINUTE), true).show();


            }
        }, end_showDate.get(Calendar.YEAR), end_showDate.get(Calendar.MONTH), end_showDate.get(Calendar.DAY_OF_MONTH));
        datePickerDialog2.show();
    }


}
